import QtQuick 2.15
import QtQml
import "qrc:/usv_bridge/UI/compass/"

Rectangle {
    id: hdg

    //航向角度 deg，正负180
    property real get_head_angle: 32
    property real get_wind_angle: 350
    property real get_current_angle: 180
    property real get_setpoint_angle: 45
    property real set_cog_angle: 50

    //约束到0-360
    function clamp_angle(angle){
        if(angle >= 360){
            angle -= 360;
        }else if(angle < 0){
            angle += 360;
        }
        return angle
    }
    color:"#F7F7F7"
    radius: 5

    //船名
    Text {
        id: text_name
        text: qsTr("智能艇")
        font.family: ("Noto Sans")
        font.pixelSize: 16
        color: "#0070D6"
        // font.bold: true
        anchors{
            top: parent.top
            topMargin: 10
            horizontalCenter: parent.horizontalCenter
        }
    }

    Rectangle{
        id: compass
        width: 350
        height: 350
        color:"#00000000"
        anchors{
            top:text_name.bottom
            topMargin: -25
            horizontalCenter: parent.horizontalCenter
        }

        Image {
            width: 350
            height: 350
            anchors.centerIn: parent
            fillMode: Image.PreserveAspectFit
            source: "qrc:/usv_bridge/UI/compass/Compass_watchface.png"
        }

        Image{
            id: head_img
            width: 230
            height: 230
            anchors.centerIn: parent
            fillMode: Image.PreserveAspectFit
            source: "qrc:/usv_bridge/UI/compass/Heading.png"
            rotation: get_head_angle()

            function get_head_angle(){
                var initial_anlge = -24;
                var get_angle = hdg.get_head_angle;
                // print(get_angle + initial_anlge);
                return clamp_angle(get_angle + initial_anlge);
            }
        }

        Image{
            id: cog_img
            width: 230
            height: 230
            anchors.centerIn: parent
            fillMode: Image.PreserveAspectFit
            source: "qrc:/usv_bridge/UI/compass/COG.png"
            rotation: get_cog_angle()

            function get_cog_angle(){
                var initial_anlge = -37.16;
                var set_angle = hdg.set_cog_angle;
                // print(set_angle + initial_anlge);
                return clamp_angle(set_angle + initial_anlge);
            }
        }

        Image{
            id: setpoint_img
            width: 240
            height: 240
            anchors.centerIn: parent
            // anchors.fill: parent
            fillMode: Image.PreserveAspectFit
            source: "qrc:/usv_bridge/UI/compass/Setpoint.png"

            rotation: get_setpoint_angle()

            function get_setpoint_angle(){
                var initial_anlge = -30.0;
                var get_angle = hdg.get_setpoint_angle;
                // print(get_angle + initial_anlge);
                return clamp_angle(get_angle + initial_anlge);
            }
        }

        Image{
            id: current_img
            width: 250
            height: 250
            anchors.centerIn: parent
            fillMode: Image.PreserveAspectFit
            source: "qrc:/usv_bridge/UI/compass/Current.png"
            rotation: get_current_angle()

            function get_current_angle(){
                var initial_anlge = 27.28;
                var get_angle = hdg.get_current_angle;
                // print(get_angle + initial_anlge);
                return clamp_angle(get_angle + initial_anlge);
            }
        }

        Image{
            id: wind_img
            width: 220
            height: 220
            anchors.centerIn: parent
            fillMode: Image.PreserveAspectFit
            source: "qrc:/usv_bridge/UI/compass/Wind.png"

            rotation: get_wind_angle()

            function get_wind_angle(){
                var initial_anlge = 48.1;
                var get_angle = hdg.get_wind_angle;
                // print(get_angle + initial_anlge);
                return clamp_angle(get_angle + initial_anlge);
            }
        }

    }

    //Timer rotation animation
    Timer{
        interval: 500; running: true; repeat: true
        onTriggered: function(){
            //system controller vars
        }
    }

}
